﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;

namespace CodeBuilder
{
    public partial class MySQLConnectionUIControl : UserControl, IDataConnectionUIControl
    {
        private IDataConnectionProperties _connectionProperties;
        private IDataConnectionProperties Properties
        {
            get
            {
                return this._connectionProperties;
            }
        }
        private bool _loading;

        public MySQLConnectionUIControl()
        {
            InitializeComponent();
            this.RightToLeft = RightToLeft.Inherit;
        }

        public void Initialize(IDataConnectionProperties connectionProperties)
        {
            if (connectionProperties == null)
            {
                throw new ArgumentNullException("connectionProperties");
            }
            if (!(connectionProperties is MySqlConnectionProperties))
            {
                throw new ArgumentException(("OracleConnectionUIControl_InvalidConnectionProperties"));
            }
            if (connectionProperties is OdbcConnectionProperties)
            {
                this.savePasswordCheckBox.Enabled = false;
            }
            this._connectionProperties = connectionProperties;
        }

        public void LoadProperties()
        {
            this._loading = true;
            this.txtServerName.Text = (this.Properties["Data Source"] as string);
            this.txtUserName.Text = (this.Properties["User ID"] as string);
            this.txtPassword.Text = (this.Properties["Password"] as string);
            this.savePasswordCheckBox.Checked = (bool)(this.Properties["Persist Security Info"]??false);
            this._loading = false;
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties["User ID"] = (this.txtUserName.Text ?? "").Trim();
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties["Password"] = (this.txtPassword.Text ?? "").Trim();
            }

        }

        private void txtServerName_TextChanged(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties["Data Source"] = (this.txtServerName.Text ?? "").Trim();
            }

        }

        private void savePasswordCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties["Persist Security Info"] = this.savePasswordCheckBox.Checked;
            }
        }

        private void cmbDatabase_TextChanged(object sender, EventArgs e)
        {
            if (!this._loading)
            {
                this.Properties["Initial Catalog"] = (this.cmbDatabase.Text ?? "").Trim();
            }

        }
    }
}
